// 获取本地存储中的订单id,座位id
let str = localStorage.getItem("orderId");
orderId = str.split("&")[0];
seatIds = str.split("&")[1].split(",")
let orderData;
let time;
let payFlag = true;

// 查询订单信息
$(function () {
    $.ajax({
        url: serverConfig.serverUrl + `/devs/order/${orderId}`,
        type: 'get',
        async: false,
        success: function (result) {
            if (result.code == 200) {
                orderData = result.data;
                time = orderData.createTime;
                payEnd()
                document.getElementById("number").innerText = `项目:  票无忧电影-订单编号:${orderData.number}`
                document.getElementsByClassName("pay-span")[0].innerText = `${orderData.total}`
                document.getElementsByClassName("present-span")[0].innerText = `${orderData.total}`
            } else {
                console.log(result)
            }
        }
    })
})

// 支付请求
function pay() {
    let payCheckFlag = false;
    let payCheckArr = document.getElementsByName("pay");
    for (let i = 0; i < payCheckArr.length; i++) {
        if (payCheckArr[i].checked) {
            payCheckFlag = true;
            break;
        }
    }
    if (payFlag) {
        if (payCheckFlag) {
            $.ajax({
                type: 'get',
                url: serverConfig.serverUrl + `/devs/alipay/payTicket`,
                data: {userId: orderData.userId, orderNum: orderData.number, price: orderData.total,time:null},
                success: function (alipayRedirectHtml) { // 创建一个新的div元素
                    var div = document.createElement("div"); // 将后台返回的支付宝支付页面的HTML内容赋值给div的
                    div.innerHTML = alipayRedirectHtml; // 获取div中的form元素
                    var form = div.querySelector("form"); // 将form添加到文档中
                    document.body.appendChild(form); // 提交form表单，实现页面跳转到支付宝支付页面
                    form.submit();
                }
            })
        } else
            layer.msg('请选择支付方式!', {icon: 2})
    } else
        layer.msg('支付超时，请重新选座!', {icon: 2})
}

function payEnd() {
    let now = new Date();//现在的时间
    let oldTime = now.getTime();//现在时间的毫秒数

    let newTime = (new Date(time)).getTime() + 60 * 1000
    let minutes = Math.floor((newTime - oldTime) / 1000 / 60)
    let seconds = Math.floor((newTime - oldTime) / 1000 - minutes * 60)

    let jugeTime = (newTime - oldTime);
    if (jugeTime <= 0) {
        payFlag = false;
        clearInterval(ms)
        document.getElementsByClassName("pay-time")[0].innerText = `00:00`;
        timeOut();
    } else {
        let str = '0'
        if (minutes < 10)
            minutes = str + minutes

        if (seconds < 10)
            seconds = str + seconds

        document.getElementsByClassName("pay-time")[0].innerText = `${minutes}:${seconds}`;
    }
}

let ms = setInterval(payEnd, 1000)

function payfor(obj) {
    let aliPay = document.querySelector(".Alipay-div");
    let wechatPay = document.querySelector(".Wechat-div");
    let imgAliPay = document.querySelector(".payStyleAlipay-div");
    let imgWechatPay = document.querySelector(".payStyleWechat-div");
    if (obj.className == 'layui-col-md2 Alipay-div') {
        $(wechatPay).css("border-top", "none").css("border-right", "none").css("border-bottom", "");
        $(aliPay).css("border-bottom", "none").css("border-left", "").css("border-top", "");
        $(imgAliPay).css("display", "");
        $(imgWechatPay).css("display", "none");
    } else {
        $(aliPay).css("border-left", "none").css("border-top", "none").css("border-bottom", "1px solid pink")
        $(wechatPay).css("border-bottom", "none").css("border-top", "1px solid pink").css("border-right", "1px solid pink");
        $(imgAliPay).css("display", "none");
        $(imgWechatPay).css("display", "");
    }
}
